#!/usr/bin/python
# GalacticModel.py

# Library modules
from time import asctime
from time import strftime
from os import linesep
#import numpy


# Intake parameters
# Units/types noted are the FINAL units/types, after run through alterpars.

# SIMULATION PARAMETERS
import GM_simulationparameters as spars
simpars = {# Parameters directly set by user
           'nodes' : spars.nodes,
           'subnodes' : spars.subnodes,
           'codedir' : spars.codedir,
           'outdir' : spars.outdir,
           'pydir' : spars.pydir,
           #'Nptsxy' : spars.Nptsxy, # int
           'scale' : spars.scale, # pc float
           'galactocentric' : spars.galactocentric, # Not yet implemented
           'LensedMag' : spars.LensedMag, # float # Not yet implemented
           'SourceMag' : spars.SourceMag, # float
           'Magnification' : spars.Magnification, # float # Not yet implemented
           'brightlimit' : spars.brightlimit, # int
           'lres' : spars.lres,
           'bres' : spars.bres,
           'dres' : spars.dres,
           'band' : spars.band,
           'c0_V' : spars.c0_V,
           'rho0_V' : spars.rho0_V,
           'c0_I' : spars.c0_I,
           'rho0_I' : spars.rho0_I,
           'c0' : '',
           'rho0' : '',
           'fitcrho' : spars.fitcrho,
           'fixedrho' : spars.fixedrho,
           'fixedc' : spars.fixedc,
           'latitudeweight' : spars.latitudeweight,
           'latitudeweightexternal' : '',
           'latitudeweightinternal' : '',
           'interpolateMv' : spars.interpolateMv,
           'intervals' : spars.intervals,
           'ApparentLimits' : spars.ApparentLimits,
           'interpolateMass' : spars.interpolateMass,
           'maxmassinterval' : spars.maxmassinterval,
           # Derived parameters
           'ScaleZ' : '', # float pc
           'dlocs' : '', # float index-like array
           'llocs' : '', # float index-like array
           'blocs' : '', # float index-like array
           'delds' : '',
           'dells' : '',
           'delbs' : '',
           'delMvs' : '', #### Not supported yet
           'delMs' : '', #### Not supported yet
           'Nptsdlb' : '', # int
           'PtScaled' : '', # float pc
           'PtScalela' : '', # float radians
           'PtScaleba' : '', # float radians
#           '' : spars.,
#           '' : '',
           }

# PHYSICAL PARAMETERS
import GM_physicalparameters as ppars
phypars = {# From Han
           'R0' : ppars.R0, # pc float
           'hR' : ppars.hR, # pc float
           'hdz' : ppars.hdz, # pc float
           'ScaleList' : ppars.ScaleList, # pc float array
           'bulk_vels' : ppars.bulk_vels, #pc/yr float array
           'sigma_vels' : ppars.sigma_vels, #pc/yr float array
           # Derived parameters, Han
           'vanillaScaleList' : '',
           'modifiedScaleList' : '',
           # Binney & Merrifield 1998
           # The luminosity function
           'MvList' : ppars.MvList, # mags array
           'LumFuncList' : ppars.LumFuncList, # stars pc^-3 mags^-1 float array
           # Derived parameters, B & M
           'delMvList' : '',
           'vanillaMvList' : '',
           'vanillaLumFuncList' : '',
           'vanilladelMvList' : '',
           'modifiedMvList' : '',
           'modifiedLumFuncList' : '',
           'modifieddelMvList' : '',
           # Cox 1999
           # The measured angular density of stars
           'bvals' : ppars.bvals, # degrees array
           'MvVals' : ppars.MvVals, # mags array
           'N_bM_V' : ppars.N_bM_V, # float array log10(stars mag**-1 deg**-2)
           'allsky_V' : ppars.allsky_V, # float array log10(stars mag**-1 deg**-2)
           'N_bM_I' : ppars.N_bM_I, # float array log10(stars mag**-1 deg**-2)
           'allsky_I' : ppars.allsky_I, # float array log10(stars mag**-1 deg**-2)
           'N_bM' : '', # float array log10(stars mag**-1 degree**-2)
           'allsky' : '', # float array log10(stars mag**-1 degree**-2)
            # Derived parameters, Cox
           'limitset' : '', # float mags array
           'delMvVals' : '', # float mags array
           'vanillaMvVals' : '', # float mags array
           'vanillalimitset' : '', # float mags array
           'vanilladelMvVals' : '', # float mags array
           'fittingMvVals' : '', # mags array
           'fittinglimitset' : '', # float mags array
           'fittingdelMvVals' : '', # float mags array
           'modifiedMvVals' : '', # float mags array
           'modifiedlimitset' : '', # float mags array
           'modifieddelMvVals' : '', # float mags array
           # Kroupa et al. 1993
           # The mass function
           'masslimits' : ppars.masslimits, # 2d Msun float array
           'massindices' : ppars.massindices, # 1d unitless float array
           'massmults' : ppars.massmults, # 1d float array
           'MvMass' : ppars.MvMass,
           # Derived parameters, Kroupa
           'delMvMass' : '',
           'limitsMvMass' : '',
           'vanillaMvMass' : '', 
           'vanilladelMvMass' : '',
           'vanillalimitsMvMass' : '',
           'modifiedMvMass' : '',
           'modifieddelMvMass' : '',
           'modifiedlimitsMvMass' : '',
           # Derived parameters, Han + Kroupa
           'MvMassScales' : '',
           'modifiedMvMassScales' : '',
           'vanillaMvMassScales' : '',
#           '' : ppars.,
#           '' : '',
           }


# Homemade modules
import GM_alterpars # OK
#import GM_nonlinear
import GM_distinction # OK
import plot_distinction # OK
import GM_LumFunc # OK
import plot_LumFunc # OK
import GM_fitc0rho0 # OK
# These modules are all secretly used by GM_fitc0rho0
#import GM_apparent # OK
#import GM_logN # OK
#import GM_N_bvals # OK
# Plot the aftermath, then delete the unloved
import plot_fitc0rho0 # OK
import plot_apparent # OK
import plot_logN # OK
import replot_logN
import plot_logN_HanStyle # OK

# Begin
print 'Beginning.'
print asctime()
print linesep

# Update parameters so that their units and datatypes are appropriate
(simpars, phypars) = GM_alterpars.alterpars(simpars, phypars)
del GM_alterpars
#simpars = GM_nonlinear.nonlinear(simpars)
#del GM_nonlinear

rhocount = 5
rhomin = 1.
rhospace = 1.
ccount = 40
cmin = 0.0
cspace = 0.05

timestamp = strftime("%Y%m%d_%H_%M_%S")

cmd = "mkdir GMS"+timestamp


nthreads = len(simpars['nodes'])*(simpars['subnodes'] - 2)
njobs = ccount*rhocount
njobsperthread = int(rhocount)/int(nthreads)
if njobsperthread*int(nthreads) < int(rhocount):
    njobsperthread = njobsperthread + 1

extdir = {'timestamp' : timestamp,
#          '' : ,
#          '' : ,
          'codeindex' : '',
          'rho_n' : 1,
          'rho_min' : '',
          'rho_space' : rhospace,
          'c_n' : njobsperthread,
          'c_min' : '',
          'c_space' : cspace,
          }

import GalacticModelSearchCCPrince as PzChmg
#### In progress hereabouts!
PzChmg.txtseg1


nodeindex = 0
subnodeindex = 0
rhoindex = 0
cindex = 0
codeindex = 0
while True:
    if subnodeindex > (simpars['subnodes'] - 3):
        subnodeindex = 0
        nodeindex = nodeindex + 1
    if cindex > (ccount - 1):
        cindex = 0
        rhoindex = rhoindex + 1
    
    extdir['codeindex'] = int(codeindex)
    extdir['rho_min'] = rhomin + rhoindex*rhospace
    extdir['c_min'] = cmin + cindex*cspace

    cindex = cindex + njobsperthread
    subnodeindex = subnodeindex + 1
    codeindex = codeindex + 1








print "I have sent all", codeindex, "jobs to the threads!"
print asctime()
print linesep

